package com.niiwoo.civet.trade.service.local.newRefund.notify.dubbo;

import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.entity.RefundNotifyRecord;
import com.niiwoo.civet.trade.dao.mapper.ProjectInvestorPlanMapperExt;
import com.niiwoo.civet.trade.dto.ProjectInvestorPlanAmountSumDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.RefundNotifyHandle;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyHandleResult;
import com.niiwoo.civet.trade.service.local.newRefund.notify.enums.RefundNotifyEnum;
import com.niiwoo.tripod.tiancheng.component.TianchengDataFlowService;
import com.niiwoo.tripod.tiancheng.request.df.InvestRefundRequest;
import com.niiwoo.tripod.tiancheng.response.common.TianchengUapResponse;

import lombok.extern.slf4j.Slf4j;

/**
 * 天称-投资人回款数据回流
 * 
 * 触发条件：还款
 * @author: kevin.jia
 * @version 1.0.0
 */
@Slf4j
@Service
public class TianChengInvestorReceiveNotify implements RefundNotifyHandle {

	@Autowired
	private ProjectInvestorPlanMapperExt projectInvestorPlanMapper;
	
	@Autowired
	private TianchengDataFlowService tianchengDataFlowService;

	@Override
	public RefundNotifyHandleResult handle(RefundNotifyDTO refundNotifyDTO) {
		RefundNotifyHandleResult refundNotifyHandleResult = null;
		try {
			InvestRefundRequest investRefundRequest = (InvestRefundRequest) bulidParam(refundNotifyDTO);
			log.info("天称-投资人回款数据回流，ProjectId：{}， 请求参数：{}", investRefundRequest.getInvestOrderId(), JSONObject.toJSONString(investRefundRequest));
			CompletableFuture<TianchengUapResponse> completableFuture = tianchengDataFlowService.investRefund(investRefundRequest);
			TianchengUapResponse tianchengUapResponse = completableFuture.get(3, TimeUnit.SECONDS);	
			log.info("天称-投资人回款数据回流 ，ProjectId：{}， 返回结果：{}", investRefundRequest.getInvestOrderId(), JSONObject.toJSONString(tianchengUapResponse));
			refundNotifyHandleResult = new RefundNotifyHandleResult();
		} catch (Exception e) {
			refundNotifyHandleResult = new RefundNotifyHandleResult(e.getMessage());
			log.error("天称-投资人回款数据回流异常，异常原因：{}， 请求参数：{}", e.getMessage(), JSONObject.toJSONString(refundNotifyDTO.getRefundNotifyRecord()), e);
		}
		return refundNotifyHandleResult;
	}

	@Override
	public Object bulidParam(RefundNotifyDTO refundNotifyDTO) {
		Project project = refundNotifyDTO.getProject();
		RefundNotifyRecord refundNotifyRecord = refundNotifyDTO.getRefundNotifyRecord();
		ProjectInvestorPlanAmountSumDTO projectInvestorPlanAmountSumDTO = projectInvestorPlanMapper
				.sumInvestorPlanAmountByInvestOrderId(refundNotifyRecord.getInvestOrderId());
	
		// 实际收益 = 已收利息 + 已收罚息
		BigDecimal realRevenue = projectInvestorPlanAmountSumDTO.getReceivedInterest().add(projectInvestorPlanAmountSumDTO.getReceivedPenaltyFee());
		// 实际回款 = 已收本金 + 实际收益
		BigDecimal realAmount = projectInvestorPlanAmountSumDTO.getReceivedCapital().add(realRevenue);
		
		InvestRefundRequest investRefundRequest = new InvestRefundRequest();
		investRefundRequest.setBid(project.getProjectId().toString());
		investRefundRequest.setInvestOrderId(projectInvestorPlanAmountSumDTO.getInvestOrderId().toString());
		investRefundRequest.setUserId(project.getBorrowUserId());
		investRefundRequest.setRealRevenue(realRevenue);
		investRefundRequest.setRealAmount(realAmount);
		investRefundRequest.setRealBackDate(System.currentTimeMillis() * 1000);
		
		return investRefundRequest;
	}
	
	@Override
	public RefundNotifyEnum getType() {
		return RefundNotifyEnum.TIANCHENG_INVESTOR_RECEIVE;
	}
}